﻿using System.Drawing;

namespace XYAC.Framework.MonitorTireMaterialManual.DataExporter
{
    /// <summary>
    /// 表示一个点数据列表，继承自 List&lt;PointData&gt;，用于管理和操作点数据。
    /// </summary>
    public class PointDataList : List<PointData>
    {
        /// <summary>
        /// 按X轴升序排序。
        /// </summary>
        public void AscSortForX()
        {
            base.Sort(new AscComparer());
        }

        /// <summary>
        /// 按X轴降序排序。
        /// </summary>
        public void DesSortForX()
        {
            base.Sort(new DesComparer());
        }

        /// <summary>
        /// 查找最接近指定X轴位置的点。
        /// </summary>
        /// <param name="X">指定的X轴位置。</param>
        /// <returns>最接近的点数据。</returns>
        public PointData FindXNearestPoint(float X)
        {
            if (base.Count == 0)
            {
                return null;
            }
            PointData data = base[0];
            for (int i = 0; i < base.Count; i++)
            {
                if (Math.Abs(base[i].Point.X - X) < Math.Abs(data.Point.X - X))
                {
                    data = base[i];
                }
            }
            return data;
        }

        /// <summary>
        /// 设置X轴偏移量，返回一个新的点数据列表。
        /// </summary>
        /// <param name="X">X轴偏移量。</param>
        /// <returns>应用了X轴偏移量的点数据列表。</returns>
        public PointDataList SetXOffset(float X)
        {
            PointDataList list = new PointDataList();
            for (int i = 0; i < base.Count; i++)
            {
                list.Add(new PointData(new PointF(base[i].Point.X - X, base[i].Point.Y), base[i].XOffset, base[i].YOffset));
            }
            return list;
        }

        /// <summary>
        /// 获取指定X轴位置的Y轴值。
        /// </summary>
        /// <param name="X">X轴位置。</param>
        /// <returns>对应的Y轴值。</returns>
        public float Y(float X)
        {
            int num4;
            PointF point;
            PointF tf2;
            Line line;
            float num = 0f;
            int num2 = base.Count / 2;
            float x = base[num2].Point.X;
            if (X < x)
            {
                for (num4 = 0; num4 < (base.Count - 1); num4++)
                {
                    point = base[num4].Point;
                    tf2 = base[num4 + 1].Point;
                    if (X == point.X)
                    {
                        return point.Y;
                    }
                    if (X == tf2.X)
                    {
                        return tf2.Y;
                    }
                    if ((X > point.X) && (X < tf2.X))
                    {
                        line = new Line(point, tf2);
                        return ((line.K * (X - point.X)) + point.Y);
                    }
                }
                return num;
            }
            for (num4 = base.Count - 1; num4 > 0; num4--)
            {
                point = base[num4].Point;
                tf2 = base[num4 - 1].Point;
                if (X == point.X)
                {
                    return point.Y;
                }
                if (X == tf2.X)
                {
                    return tf2.Y;
                }
                if ((X < point.X) && (X > tf2.X))
                {
                    line = new Line(tf2, point);
                    return ((line.K * (X - tf2.X)) + tf2.Y);
                }
            }
            return num;
        }

        /// <summary>
        /// 获取列表中最高的Y轴值。
        /// </summary>
        public float MaxHeight
        {
            get
            {
                float y = 0f;
                for (int i = 0; i < base.Count; i++)
                {
                    if (y < base[i].Point.Y)
                    {
                        y = base[i].Point.Y;
                    }
                }
                return y;
            }
        }

        /// <summary>
        /// 获取所有点的坐标列表。
        /// </summary>
        public List<PointF> Points
        {
            get
            {
                List<PointF> list = new List<PointF>();
                for (int i = 0; i < base.Count; i++)
                {
                    list.Add(base[i].Point);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取所有点的Y轴值列表。
        /// </summary>
        public List<float> PointsY()
        {
            List<float> Y_List = new List<float>();
            foreach (PointF p in this.Points)
            {
                Y_List.Add(p.Y);
            }
            return Y_List;
        }

        /// <summary>
        /// 获取列表的尺寸，宽度为X轴范围，高度为最大Y轴值。
        /// </summary>
        public SizeF Size
        {
            get
            {
                float width = 0f;
                float height = 0f;
                if (base.Count < 2)
                {
                    return new SizeF(0f, 0f);
                }
                width = base[base.Count - 1].Point.X - base[0].Point.X;
                for (int i = 0; i < base.Count; i++)
                {
                    if (height < base[i].Point.Y)
                    {
                        height = base[i].Point.Y;
                    }
                }
                return new SizeF(width, height);
            }
        }

        /// <summary>
        /// 实现按X轴升序排序的比较器。
        /// </summary>
        private class AscComparer : IComparer<PointData>
        {
            public int Compare(PointData P1, PointData P2)
            {
                if (P1 == null)
                {
                    if (P2 == null)
                    {
                        return 0;
                    }
                    return -1;
                }
                if (P2 == null)
                {
                    return 1;
                }
                if (P1.Point.X < P2.Point.X)
                {
                    return -1;
                }
                if (P1.Point.X > P2.Point.X)
                {
                    return 1;
                }
                return 0;
            }
        }

        /// <summary>
        /// 实现按X轴降序排序的比较器。
        /// </summary>
        private class DesComparer : IComparer<PointData>
        {
            public int Compare(PointData P1, PointData P2)
            {
                if (P1 == null)
                {
                    if (P2 == null)
                    {
                        return 0;
                    }
                    return -1;
                }
                if (P2 == null)
                {
                    return 1;
                }
                if (P1.Point.X > P2.Point.X)
                {
                    return -1;
                }
                if (P1.Point.X < P2.Point.X)
                {
                    return 1;
                }
                return 0;
            }
        }
    }

}
